package com.shop.servlet;

import com.shop.model.Student;
import com.shop.model.Teacher;
import com.shop.service.IStudentService;
import com.shop.service.ITeacherService;
import com.shop.service.StudentService;
import com.shop.service.TeacherService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;


@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    IStudentService istudentService = new StudentService();
    ITeacherService iteacherService = new TeacherService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        req.setCharacterEncoding("UTF-8");

        // 获取用户名、密码和验证码
        String studentId = req.getParameter("username");
        String password = req.getParameter("password");
        String inputCaptcha = req.getParameter("captcha");
        String serverCaptcha = req.getParameter("serverCaptcha");
        System.out.println("LoginServlet:29:"+serverCaptcha);

        System.out.println("LoginServlet:26:" + studentId + " " + password); // 调试

        if (serverCaptcha == null || !serverCaptcha.equalsIgnoreCase(inputCaptcha)) {
            // 验证失败处理
            req.setAttribute("error", "验证码错误");
            req.getRequestDispatcher("login.jsp").forward(req, resp);
        }

        System.out.println("LoginServlet:44"+password.length());

        if(studentId.length()==5){
            Teacher teacher = iteacherService.login(studentId,password);
            if (teacher == null) {
                req.setAttribute("error", "用户名或密码错误");
                req.getRequestDispatcher("login.jsp").forward(req, resp);
                return;
            }
            if (teacher != null && serverCaptcha != null && serverCaptcha.equalsIgnoreCase(inputCaptcha)) {
                // 4. 登录成功，存储用户信息到 session
                HttpSession session = req.getSession();
                session.setAttribute("teacher", teacher);
                System.out.println("LoginServlet：57：登录成功"); // 调试

                req.setAttribute("studentId", studentId);
                req.getRequestDispatcher("welcome_t.jsp").forward(req, resp);
            }
        }

        else if(studentId.length() !=5) {
            // 3. 验证用户名和密码
            Student student = istudentService.login(studentId, password);
            if (student == null) {
                req.setAttribute("error", "用户名或密码错误");
                req.getRequestDispatcher("login.jsp").forward(req, resp);
                return;
            }
            if (student != null && serverCaptcha != null && serverCaptcha.equalsIgnoreCase(inputCaptcha)) {
                // 4. 登录成功，存储用户信息到 session
                HttpSession session = req.getSession();
                session.setAttribute("student", student);
                System.out.println("LoginServlet：32：登录成功"); // 调试


                // 6. 跳转到欢迎页
                req.setAttribute("studentId", studentId);
                req.getRequestDispatcher("welcome.jsp").forward(req, resp);
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("login.jsp").forward(request, response);
    }
}
